﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Admin_QuanlyTu : System.Web.UI.Page
{
    String sCnn = ConfigurationManager.ConnectionStrings["cnTudienOnline"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlTu_napdulieu();
            napDulieuGvNghiatu();
        }
    }
    private void ddlTu_napdulieu()
    {
        // Khai báo các biến để thao tác với CSDL
        SqlConnection cnQuanlyTudien = null;
        SqlCommand cmQuanlyTudien = null;
        SqlDataAdapter daQuanlyTudien = null;
        try
        {
            // Khởi tạo các đối tượng
            cnQuanlyTudien = new SqlConnection(sCnn);
            cmQuanlyTudien = new SqlCommand("spTu_getByID", cnQuanlyTudien);
            cmQuanlyTudien.CommandType = CommandType.StoredProcedure;
            int iTuID = 0;
            cmQuanlyTudien.Parameters.Add(new SqlParameter("@PK_iTuID", iTuID));
            // Khởi tạo DataAdapter để điền dữ liệu vào cho Dataset
            daQuanlyTudien = new SqlDataAdapter(cmQuanlyTudien);
            DataSet dsTudien = new DataSet();
            daQuanlyTudien.Fill(dsTudien);
            if (dsTudien.Tables.Count > 0 && dsTudien.Tables[0].Rows.Count > 0)
            {
                ddlTu.DataSource = dsTudien;
                ddlTu.DataTextField = "sTu";
                ddlTu.DataValueField = "PK_iTuID";
                ddlTu.DataBind();
            }
        }
        catch (Exception ex)
        {
            lblThongbao.Text = ex.Message;
        }
        finally
        {
            cnQuanlyTudien.Close();
            cnQuanlyTudien.Dispose();
            cmQuanlyTudien.Dispose();
            daQuanlyTudien.Dispose();
        }
    }
    private void napDulieuGvNghiatu()
    {
        SqlConnection cn = null;
        SqlCommand cm = null;
        SqlDataAdapter da = null;
        try
        {
            cn = new SqlConnection(sCnn);
            cm = new SqlCommand("spNghiatu_getBytuID", cn);
            cm.CommandType = CommandType.StoredProcedure;
            int iTuID = Convert.ToInt32(ddlTu.SelectedValue);
            cm.Parameters.Add(new SqlParameter("@FK_iTuID", iTuID));
            da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables.Count > 0&&ds.Tables[0].Rows.Count>0)
                {
                    gvNghiatu.DataSource = ds;
                    gvNghiatu.DataKeyNames = new string[] { "PK_iNghiatuID" };
                    gvNghiatu.DataBind();
                }
            }
        catch (Exception ex)
            {
                lblThongbao.Text = ex.Message;
            }
            finally
            {
                cn.Close();
                cn.Dispose();
                cm.Dispose();
                da.Dispose();
            }
    }       


    protected void ddlTu_SelectedIndexChanged(object sender, EventArgs e)
    {
        napDulieuGvNghiatu();
        pnEdit.Visible = false;
    }
    private void napForm(int iNghiatuID)
    {
        // Khai báo các biến để thao tác với CSDL
        SqlConnection cnQuanlyTudien = null;
        SqlCommand cmQuanlyTudien = null;
        SqlDataAdapter daQuanlyTudien = null;
        try
        {
            // Khởi tạo các đối tượng
            cnQuanlyTudien = new SqlConnection(sCnn);
            cmQuanlyTudien = new SqlCommand("spNghiaTu_getbyID", cnQuanlyTudien);
            cmQuanlyTudien.CommandType = CommandType.StoredProcedure;

            cmQuanlyTudien.Parameters.Add(new SqlParameter("@PK_iNghiaTuID", iNghiatuID));
            // Khởi tạo DataAdapter để điền dữ liệu vào cho Dataset
            daQuanlyTudien = new SqlDataAdapter(cmQuanlyTudien);
            DataSet dsTudien = new DataSet();
            daQuanlyTudien.Fill(dsTudien);
            if (dsTudien.Tables.Count > 0 && dsTudien.Tables[0].Rows.Count > 0)
            {
                //txtTu.Text = dsTudien.Tables[0].Rows[0]["sTu"].ToString();
                txtNghia.Text = dsTudien.Tables[0].Rows[0]["sNoidung"].ToString();
            }
            btnLuu.CommandArgument = iNghiatuID.ToString();
        }
        catch (Exception ex)
        {
            lblThongbao.Text = ex.Message;
        }
        finally
        {
            cnQuanlyTudien.Close();
            cnQuanlyTudien.Dispose();
            cmQuanlyTudien.Dispose();
            daQuanlyTudien.Dispose();
        }
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        SqlConnection cn = null;
        SqlCommand cm = null;
        try
        {
            cn = new SqlConnection(sCnn);
            cn.Open();
            if(btnLuu.CommandName.ToUpper()=="EDIT")
            {
                cm = new SqlCommand("spNghiatu_updateByID", cn);
                cm.CommandType = CommandType.StoredProcedure;
                int iNghiatuID = Convert.ToInt16(btnLuu.CommandArgument);
                cm.Parameters.Add(new SqlParameter("@PK_iNghiatuID",iNghiatuID));
                cm.Parameters.Add(new SqlParameter("@sNoidung",txtNghia.Text));
            }
            cm.ExecuteNonQuery();
        }catch (Exception ex)
            {
                lblThongbao.Text = ex.Message;
            }   
        finally
        {
                cn.Close();
                cn.Dispose();
                cm.Dispose();
        }
            napDulieuGvNghiatu();
            pnEdit.Visible = false;
    }
    protected void btnBoqua_Click(object sender, EventArgs e)
    {
        pnEdit.Visible = false;
    }
    protected void lbtnThemmoi_Click(object sender, EventArgs e)
    {

    }
    protected void txtTu_TextChanged(object sender, EventArgs e)
    {

    }

    protected void gvNghiatu_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        try
        {
            int iNghiaTuID = Convert.ToInt16(gvNghiatu.DataKeys[e.NewSelectedIndex].Values["PK_iNghiatuID"].ToString());
            pnEdit.Visible = true;
            napForm(iNghiaTuID);
            pnEdit.Visible = true;
            btnLuu.CommandName = "EDIT";
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}